export function isArray(obj) {
    return obj && obj.constructor === Array
}

export function isObject(obj) {
    return obj && obj.constructor === Object
}

export function isEmpty(v) {
    if (v === null || v === undefined) return true
    return typeof v === 'string' && v.trim() === '';
}

export function downloadFileByBlob(blob, fileName = "file") {
    let blobUrl = window.URL.createObjectURL(blob)
    let link = document.createElement('a')
    link.download = decodeURI(fileName)
    link.style.display = 'none'
    link.href = blobUrl
    // 触发点击
    document.body.appendChild(link)
    link.click()
    // 移除
    document.body.removeChild(link)
}

export function equals(a, b) {
    if (typeof a !== typeof b) {
        return false;
    }
    if (typeof a.equals === 'function') {
        return a.equals(b)
    }
    if (typeof b.equals === 'function') {
        return b.equals(a)
    }
    return JSON.stringify(a) === JSON.stringify(b)
}

// 将 Blob 转换为 base64URL
export function blobToBase64URL(blob) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = () => {
            const dataUrl = reader.result;
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');
            const img = new Image();
            img.onload = () => {
                canvas.width = img.width;
                canvas.height = img.height;
                ctx.drawImage(img, 0, 0);
                const base64URL = canvas.toDataURL(blob.type);
                resolve(base64URL);
            };
            img.onerror = reject;
            img.src = dataUrl;
        };
        reader.onerror = reject;
        reader.readAsDataURL(blob);
    });
}

export function uuid() {
    let d = new Date().getTime();
    if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
        d += performance.now(); // use high-precision timer if available
    }
    const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        const r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}
export function fileToBlob(file, callback) {
    // 读取文件内容
    var reader = new FileReader();
    reader.readAsArrayBuffer(file);

    // 创建Blob对象
    reader.onload = function (event) {
        var blob = new Blob([event.target.result], { type: file.type });
        // 调用回调函数并传递Blob对象
        callback(blob);
    };
}
export function formatBytes(bytes, decimals = 2) {
    if (bytes === 0) return '0 Bytes';
    const k = 1024;
    const dm = decimals < 0 ? 0 : decimals;
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
export function blobOrFileToArray(blob) {
    return new Promise((resolve, reject) => {
        var reader = new FileReader();

        reader.onloadend = function () {
            var arrayBuffer = reader.result;
            var uint8Array = new Uint8Array(arrayBuffer);
            var array = Array.from(uint8Array);
            resolve(array);
        };

        reader.onerror = function () {
            reject(new Error('Failed to read Blob as array'));
        };

        reader.readAsArrayBuffer(blob);
    });
}

export function convertToCamelCase(tableName, isPascalCase) {
    // 分割字符串并将每个单词的首字母大写
    const words = tableName.split('_').map(word => {
      return word.charAt(0).toUpperCase() + word.slice(1);
    });
  
    // 如果要转换为小驼峰，则将第一个单词的首字母小写
    if (!isPascalCase) {
      words[0] = words[0].charAt(0).toLowerCase() + words[0].slice(1);
    }
  
    return words.join('');
  }